/*
 * Created on Oct 22, 2003
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package org.vectrics.company;

import org.vectrics.common.db.JdbcTarget;
import org.vectrics.common.db.ProcessException;
import java.sql.ResultSet;
import java.util.Iterator;
import java.util.Collection;
import java.util.List;
import java.util.Vector;
import org.apache.log4j.Logger;
import java.text.SimpleDateFormat;



public class FindAllCompaniesJdbc implements JdbcTarget {
	private transient static Logger log = Logger.getLogger(FindAllCompaniesJdbc.class);
	private static SimpleDateFormat sqlDateFormat = new SimpleDateFormat("yyyy-MM-dd");
	
	private String queryStr = "";
	private Vector userSet = new Vector();		

	public FindAllCompaniesJdbc(Collection roles) {         
		       	
		log.debug("FindUsersWithRoleJdbc() - constructing query");
		String orderClause = " order by u.last_name, u.first_name, u.middle_name";
		String whereClause = ""; 
		if (roles.size() > 0) {
			whereClause += " where";
			whereClause += " (u.user_name = r.user_name) and (";
			Iterator iter = roles.iterator();
			int index = 0;
			while (iter.hasNext()) {
				String roleName = (String)iter.next();
				if (index > 0)
					whereClause += " or";
				whereClause += " (r.role_name = '" + roleName + "')";
				index++;
			}
			whereClause += ")";            
		}
        
		queryStr = "select distinct u.id"
				+ " from user_priv r, user u" 
				+ whereClause 
				+ orderClause;

		if (log.isDebugEnabled()) {
			log.debug("FindUsersWithRoleJdbc() - queryStr = " + queryStr);
			log.debug("FindUsersWithRoleJdbc() - showing roles...");
			Iterator iter = roles.iterator();
			while (iter.hasNext()) {
				String role = (String)iter.next();
				log.debug("FindUsersWithRoleJdbc() - role = " + role);
			}
		}
	}
	

	public List getUserSet() {
		return(userSet);
	}
	
	public void processResults(ResultSet results) throws ProcessException {
		try {
			while (results.next()) {
				String userId = results.getString("uid");
				userSet.add(userId);
			}
		}
		catch (java.sql.SQLException se) {
			log.error("getUsersWithRole() - SQL Exception while processing results list", se);
		}
	}

	
	public String getQuery() {
		return(queryStr);		
	}
}

